package com.game.modular.gameCore;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseCore  implements Serializable {
    @TableField(exist = false)
    public Map<Integer,Between> resTargetValueBetween;
    @TableField(exist = false)
    public List<Long> resValue;

    public void inItTargetValueBetween(String targetValue) {
        resTargetValueBetween = new HashMap<>();
        if (targetValue.contains(",")){
            int index = 1;
            for (String s : targetValue.split(",")) {
                if (s.contains("-")){
                    BaseCore.Between build = BaseCore.Between.builder().start(Integer.parseInt(s.split("-")[0]))
                            .end(Integer.parseInt(s.split("-")[1]))
                            .build();
                    resTargetValueBetween.put(index,build);

                }else {
                    int i = Integer.parseInt(s);
                    BaseCore.Between build = BaseCore.Between.builder()
                            .start(i)
                            .end(i)
                            .build();
                    resTargetValueBetween.put(index,build);
                }
                index++;
            }
        }else if (targetValue.contains("-")){
            if (targetValue.contains("-")){
                String[] split = targetValue.split("-");
                BaseCore.Between build = BaseCore.Between.builder().start(Integer.parseInt(split[0]))
                        .end(Integer.parseInt(split[1]))
                        .build();
                resTargetValueBetween.put(1,build);
            }else {
                int i = Integer.parseInt(targetValue);
                BaseCore.Between build = BaseCore.Between.builder()
                        .start(i)
                        .end(i)
                        .build();
                resTargetValueBetween.put(1,build);
            }
        }
    }
    public void init(String value) {
        resValue = new ArrayList<>();
        if (value.contains(",")){
            for (String s : value.split(",")) {
                resValue.add(Long.parseLong(s));
            }
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class Between{
        @TableField(exist = false)
        private Integer start;
        @TableField(exist = false)
        private Integer end;

        public boolean isIn(Long value){
            return value >= start && value <= end;
        }
        public boolean isIn(Integer value){
            return value >= start && value <= end;
        }
    }
}
